package com.interesting.client.UI.setting;

import java.util.HashMap;
import java.util.Map;

import org.json.JSONException;
import org.json.JSONObject;

import android.app.Activity;
import android.content.Intent;
import android.content.SharedPreferences;
import android.graphics.Paint;
import android.os.AsyncTask;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.animation.Animation;
import android.view.animation.TranslateAnimation;
import android.widget.Button;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.actionbarsherlock.view.MenuItem;
import com.interesting.R;
import com.interesting.base.core.BaseActivity;
import com.interesting.base.http.HttpUtil;
import com.interesting.base.utils.Constant;
import com.interesting.client.UI.commonActivity.UIUtil;
import com.interesting.client.application.AppHolder;

/** 注册界面 */
public class RegisterActivity extends BaseActivity {

	private TextView login;
	private LinearLayout layout;
	private EditText username, pasd, pasdagain;
	private Button submit;

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.setting_register_activity);
		getSupportActionBar().setCustomView(R.layout.text_actionbar_custom_view);

		setTitle(getResources().getString(R.string.register_title));

		findview();

		OnClickListener listener = new OnClickListener() {

			@Override
			public void onClick(View v) {
				switch (v.getId()) {
				case R.id.setting_register_activity_tologin: // 去登录
					finish();
					break;
				case R.id.setting_register_activity_submit: // 提交注册
					toRegister(username.getText().toString(), pasd.getText().toString(), pasdagain.getText().toString());

					break;
				}

			}
		};

		login.setOnClickListener(listener);
		submit.setOnClickListener(listener);

	}

	private void findview() {

		login = (TextView) findViewById(R.id.setting_register_activity_tologin);
		layout = (LinearLayout) findViewById(R.id.setting_register_activity_layout);
		username = (EditText) findViewById(R.id.setting_register_activity_username);
		pasd = (EditText) findViewById(R.id.setting_register_activity_password);
		pasdagain = (EditText) findViewById(R.id.setting_register_activity_psdagain);
		submit = (Button) findViewById(R.id.setting_register_activity_submit);

		login.getPaint().setFlags(Paint.UNDERLINE_TEXT_FLAG);// 下划线
	}

	/** 注册事件 */
	private void toRegister(String name, String password, String confirmPasd) {
		if (name.equals("") || password.equals("") || confirmPasd.equals("")) {
			rock();
		} else if (!password.equals(confirmPasd)) {
			UIUtil.showLongToast(getApplicationContext(), "两次密码不一样，请修改");
		} else {
			UIUtil.showWaitDialog(getSupportFragmentManager());
			new RegisterAsyncTask().execute(name, password);
		}
	}

	/** 动画 */
	private void rock() {
		TranslateAnimation rock = new TranslateAnimation(-7, 7, 0, 0);
		rock.setDuration(100);
		rock.setRepeatCount(6);
		rock.setRepeatMode(Animation.REVERSE);
		layout.startAnimation(rock);
	}

	@Override
	public boolean onOptionsItemSelected(MenuItem item) {

		return super.onOptionsItemSelected(item);
	}

	/** 注册异步 */
	class RegisterAsyncTask extends AsyncTask<String, Void, String> {

		@Override
		protected String doInBackground(String... params) {
			Map<String, String> request = new HashMap<String, String>();
			request.put("act", "registerUser");
			request.put("username", params[0]);
			request.put("password", params[1]);
			request.put("imei", AppHolder.getInsatnce().getImei(getApplicationContext()));
			request.put("phone", AppHolder.getInsatnce().getPhone(getApplicationContext()));

			return HttpUtil.post(request);
		}

		@Override
		protected void onPostExecute(String result) {
			JSONObject response = HttpUtil.parseResult(result);
			if (response != null) {
				UIUtil.hideDialog(getSupportFragmentManager());
				try {
					AppHolder.getInsatnce().user.setUserId(response.getString("userId"));
				} catch (JSONException e) {

					e.printStackTrace();
				}
				/** 只保存用户名到本地 */
				SharedPreferences input = getSharedPreferences(Constant.SHARE_PREFERENCE, Activity.MODE_PRIVATE);
				SharedPreferences.Editor input_editor = input.edit();
				input_editor.putString(Constant.USERNAME, username.getText().toString());
				input_editor.commit();

				Intent loginIntent = new Intent(getApplicationContext(), LoginActivity.class);
				startActivity(loginIntent);
				finish();
			} else {
				UIUtil.hideDialog(getSupportFragmentManager());
				UIUtil.showToast(getApplicationContext(), "注册失败，换个昵称试试");
			}
			super.onPostExecute(result);
		}
	}
}
